﻿@model List<WebEntities.Models.WebInfo.News>

@{
    ViewBag.Title = "网站管理平台_新闻管理视图";
}

@section HeadContent
{
    <script type="text/javascript">
        var renderStatus = function (value) {
            var result = "";
            switch (value) {
                case 1:
                    result = "显示";
                    break;
                case 2:
                    result = "隐藏";
                    break;
                default:
                    break;
            }
            return result;
        };

        var renderImportant = function (value) {
            var result = "";
            switch (value) {
                case 1:
                    result = "一般";
                    break;
                case 2:
                    result = "重要";
                    break;
                default:
                    break;
            }
            return result;
        };

        var triggerClick = function (field, trigger, index) {
            var value = field.getValue();
            var store = field.up("grid").getStore();
            if (index == 0) {
                field.setValue('');
                store.clearFilter();
                trigger.hide();
            }
            else {
                var value = field.getValue();
                if (value.length > 0) {
                    store.filter({
                        id: store.proxy.filterParam,
                        property: store.proxy.filterParam,
                        value: value
                    });
                    field.getTrigger(0).show();
                }
            }
        };

        var onSpecialKey = function (field, e) {
            if (e.getKey() === e.ENTER) {
                triggerClick(field, null, 1);
                e.stopEvent();
            }
        };
    </script>
}

@(Html.X().Menu()
    .ID("Grid1CtxMenu")
    .Items(
        Html.X().MenuItem().Text("编辑新闻").Icon(Icon.BookEdit)
            .DirectEvents(de =>
            {
                de.Click.Action = "EditNewsWindow";
                de.Click.ExtraParams.Add(new Parameter("newsId", "this.parentMenu.dataRecord.data.NewsID", ParameterMode.Raw));
            }),
            Html.X().MenuItem().Text("删除新闻").Icon(Icon.Cross).Listeners(ls => ls.Click.Handler = "App.BtnDeleteSel.getEl().dom.click()")
    )
)

@Html.X().Hidden().ID("DeleteHint").Text("是否要删除选中的新闻？")

@(Html.X().Viewport()
        .Layout("fit")
        .Items(
            Html.X().GridPanel()
                .ID("GridPanel1")
                .Title("新闻管理视图")
                .Store(
                    Html.X().StoreForModel()
                        .RemoteFilter(true)
                        .AutoSync(true)
                        .PageSize(50)
                        .ShowWarningOnFailure(false)
                        .Proxy(proxy => proxy.Add(Html.X().AjaxProxy()
                            .Url(Url.Action("GetAllNewsByPage"))
                            .Reader(Html.X().JsonReader().Root("data"))
                            .Writer(writer => new JsonWriter { AllowSingle = true, Root = "data" })
                            .API(api => { api.Destroy = Url.Action("DeleteNews"); })
                            .ExtraParams(ep =>
                            {
                                Parameter p1 = new Parameter
                                {
                                    Name = "cataId",
                                    Value = ViewBag.CataID.ToString(),
                                    Mode = ParameterMode.Raw
                                };
                                ep.Add(p1);
                            })
                            .FilterParam("query")
                        ))
                        .Listeners(ls => ls.Write.Fn = "ext.net.comjs.onGridChange")
                )
                .ColumnModel(
                    Html.X().RowNumbererColumn(),
                    Html.X().Column().DataIndex(Model, m => m.NewsID).Text("新闻ID"),
                    Html.X().Column().DataIndex(Model, m => m.NewsTitle).Text("新闻标题"),
                    Html.X().Column().DataIndex(Model, m => m.SubTitle).Text("副标题"),
                    Html.X().Column().DataIndex(Model, m => m.NewsTypeName).Text("新闻类别"),
                    Html.X().Column().DataIndex(Model, m => m.Keywords).Text("关键字"),
                    Html.X().Column().DataIndex(Model, m => m.CataName).Text("所属新闻目录"),
                    Html.X().Column().DataIndex(Model, m => m.SortNum).Text("序号"),
                    Html.X().Column().DataIndex(Model, m => m.ViewCount).Text("阅读次数"),
                    Html.X().Column().DataIndex(Model, m => m.NewsStatus).Text("搜索文章").Renderer("renderStatus"),
                    Html.X().Column().DataIndex(Model, m => m.Important).Text("重要性").Renderer("renderImportant"),
                    Html.X().DateColumn().DataIndex(Model, m => m.CreateTime).Text("创建时间").Format("yyyy-MM-dd HH:mm:ss").Width(120),
                    Html.X().Column().DataIndex(Model, m => m.Creator).Text("创建者"),
                    Html.X().DateColumn().DataIndex(Model, m => m.PubTime).Text("发布时间").Format("yyyy-MM-dd").Width(120),
                    Html.X().Column().DataIndex(Model, m => m.Publisher).Text("作者"),
                    Html.X().DateColumn().DataIndex(Model, m => m.LastUpdated).Text("最后更新时间").Format("yyyy-MM-dd HH:mm:ss").Width(120)
                 )
                .SelectionModel(Html.X().CheckboxSelectionModel().Mode(SelectionMode.Multi))
                .Listeners(ls =>
                {
                    ls.ItemContextMenu.Handler = "e.preventDefault(); App.Grid1CtxMenu.dataRecord = this.store.getAt(index); App.Grid1CtxMenu.showAt(e.getXY());";
                })
                .View(Html.X().GridView().StripeRows(true))
                .DirectEvents(de =>
                {
                    de.ItemDblClick.Action = "EditNewsWindow";
                    de.ItemDblClick.ExtraParams.Add(new Parameter("newsId", "record.data['NewsID']", ParameterMode.Raw));
                })
                .TopBar(
                    Html.X().Toolbar()
                        .Items(
                            Html.X().Button().Text("新建新闻").Icon(Icon.Add)
                                .DirectEvents(de =>
                                {
                                    de.Click.Action = "NewNewsWindow";
                                    de.Click.ExtraParams.Add(new Parameter("cataId", ViewBag.CataID.ToString(), ParameterMode.Raw));
                                }),
                            Html.X().Button().ID("BtnDeleteSel").Text("删除新闻").Icon(Icon.Cross)
                                .Listeners(
                                    ls => ls.Click.Fn = "ext.net.comjs.deleteGridSelected"
                                ),
                            Html.X().ToolbarFill(),
                            Html.X().TriggerField().FieldLabel("查找新闻").Width(400).LabelWidth(60)
                                .Triggers(
                                    Html.X().FieldTrigger().Icon(TriggerIcon.Clear).HideTrigger(true),
                                    Html.X().FieldTrigger().Icon(TriggerIcon.Search)
                                )
                                .Listeners(ls =>
                                {
                                    ls.TriggerClick.Fn = "triggerClick";
                                    ls.SpecialKey.Fn = "onSpecialKey";
                                })
                        )
                )
                .BottomBar(
                    Html.X().PagingToolbar()
                        .Items(
                            Html.X().Label("每页显示行数："),
                            Html.X().ToolbarSpacer(10),
                            Html.X().ComboBox()
                                .Width(80)
                                .Items("10", "50", "100")
                                .SelectedItems("50")
                                .Listeners(lr =>
                                {
                                    lr.Select.Fn = "ext.net.comjs.onPageSizeChange";
                                })
                        )
                )
        )
)